package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.GraphInfoService;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Component
public class ScheduledTasks {
    @Autowired
    LeaseAgreementService leaseAgreementService;

    //    查询数据库中 图片状态
    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    MinioClient client;
    @Autowired
    MinioProperties properties;

    @Autowired
    RedisTemplate redisTemplate;

    //    cron 表达式 每晚 23:00 执行一次
//    @Scheduled(cron = "0 0 23 * * *")
    public void checkLeaseAgreementStatus() {
        // 租约中的 租约结束日期 如果小于了 当前的系统时间 则自动更新为 已到期
        Date date = new Date();
        leaseAgreementService.update(new LambdaUpdateWrapper<LeaseAgreement>()
//               获取 租约状态 为 已签约 或 退租待确认 并且 租约结束日期 小于 当前系统时间
                        .lt(LeaseAgreement::getLeaseEndDate, date)
//                获取 租约状态 为 已签约 或 退租待确认
                        .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING)
//                设置 租约状态 为 已到期
                        .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED)
        );
        System.out.println("定时任务执行完成");
    }

//    @Scheduled(cron = "0/5 * * * * ?")
    public void checkRemoveMinioImage() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
//        获取到所有图片 为is_deleted=1 的
        List<String> list = graphInfoService.removeByIsDeleted();
//        遍历所有图片 获取到图片名称
        for (String urlName : list) {
//            判断图片名称是否为空
            if (!StringUtils.isEmpty(urlName)) {
//                indexOf 获取到图片的下标位置 进行截取
                String fileName = urlName.substring(urlName.indexOf("lease/") + 6);
                client.removeObject(RemoveObjectArgs.builder().bucket(properties.getBucketName()).object(fileName).build());
            }
        }

        //使用redis计算哪些图片在minio中可以删除
        String allImages = "all_images:key";
        String dbImageKey = "db_image:key";
        Set difference = redisTemplate.opsForSet().difference(allImages, dbImageKey);
        for (Object fileName : difference) {
            //到minio中删除图片
            client.removeObject(RemoveObjectArgs.builder().bucket(properties.getBucketName()).object(fileName.toString()).build());
        }
    }
}
